/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TaskPanel.java
 *
 * Created on 2011/12/31, 上午 10:37:58
 */
package elaborate.task.ui;

import elaborate.task.DefaultTaskQueueFactory;
import elaborate.task.Task;
import elaborate.task.TaskQueue;
import elaborate.task.TaskQueueEvent;
import elaborate.task.TaskQueueEventListener;

/**
 * a convenient UI class that features TaskQueue for
 * async task execution functionality
 * a loading progress bar will be automatically shown
 * when task being queued
 * @author lendle
 */
public class TaskPanel extends javax.swing.JPanel implements TaskQueueEventListener{
    private TaskQueue taskQueue=null;
    /** Creates new form TaskPanel */
    public TaskPanel() {
        initComponents();
        taskQueue=new DefaultTaskQueueFactory().newTaskQueue();
        this.taskQueue.addTaskQueueListener(this);
        this.taskQueue.start();
    }
    
    public void addTask(Task task){
        this.taskQueue.addTask(task);
    }
    
    public TaskQueue getTaskQueue(){
        return this.taskQueue;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        southPanel = new javax.swing.JPanel();
        loadingProgressBar = new elaborate.task.ui.LoadingProgressBar();

        setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 284, Short.MAX_VALUE)
        );

        add(mainPanel, java.awt.BorderLayout.CENTER);

        southPanel.setLayout(new java.awt.BorderLayout());
        southPanel.add(loadingProgressBar, java.awt.BorderLayout.EAST);

        add(southPanel, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private elaborate.task.ui.LoadingProgressBar loadingProgressBar;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel southPanel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void taskCompleted(TaskQueueEvent e) {
    }

    @Override
    public void taskAdded(TaskQueueEvent e) {
        this.loadingProgressBar.start();
    }

    @Override
    public void taskFailed(TaskQueueEvent e) {
        
    }

    @Override
    public void taskQueueEmptied(TaskQueueEvent e) {
        this.loadingProgressBar.stop();
    }

    @Override
    public void taskQueueStarted(TaskQueueEvent e) {
        
    }

    @Override
    public void taskQueueStopped(TaskQueueEvent e) {
        this.loadingProgressBar.stop();
    }

    @Override
    public void taskQueuePaused(TaskQueueEvent e) {
        
    }

    @Override
    public void taskQueueResumed(TaskQueueEvent e) {
        
    }
}
